package com.cn.learn.jpa.controller;

import com.cn.learn.jpa.domain.entity.User;
import com.cn.learn.jpa.domain.model.ResultModel;
import com.cn.learn.jpa.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName UserController
 * @Description
 * @Author JiJiang
 * @Date 2022/9/13 11:19
 * @Version 1.0
 */
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private IUserService userService;


    //region 测试接口
    @GetMapping("/save")
    public String save() {

        userService.save();

        return "save...";
    }

    //endregion


    /**
     * @MethodName findById
     * @Description 根据id查询用户
     * @Param [id]
     * @Return com.xn.wanxi.bootlearnstep7.domain.entity.User
     **/
    @GetMapping("/{id}")
    public ResultModel findAUser(@PathVariable Integer id) {

        ResultModel result = userService.getUserById(id);

        return result;

    }


    @PostMapping
    public ResultModel addAUser(User user) {

        ResultModel result = userService.saveAUser(user);
        return result;

    }


    @GetMapping
    public ResultModel findUsers(@RequestBody User user) {
        ResultModel result = userService.getAllUsers(user);
        return result;

    }

    @DeleteMapping("/{id}")
    public ResultModel cleanAUser(@PathVariable Integer id) {

        ResultModel result = userService.removeUserById(id);
        return result;
    }

    @PutMapping
    public ResultModel cleanAUser(@RequestBody User user) {

        ResultModel result = userService.modifyUserById(user);
        return result;
    }



}
